﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CustomsSystem.BLL;
using CustomsSystem.Model;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Maticsoft.Common;

namespace WEB.template
{
    public partial class feeTemplateView : System.Web.UI.Page
    {
        DataSet ds = new DataSet();


        protected void Page_Load(object sender, EventArgs e)
        {//显示货物类别
            if (!IsPostBack)
            {
                ddlGoods.DataSource = GetGoodsType();
                ddlGoods.DataTextField = "codeName";
                ddlGoods.DataValueField = "codeID";
                ddlGoods.DataBind();
            }
            Bind();
        }
        /// <summary>
        /// 绑定
        /// </summary>
        public void Bind()
        {
            string sql = "";
            if (ddlGoods.SelectedValue == "0")
                sql += " 1=1 ";
            else
                sql += " goodsType='"+ddlGoods .SelectedValue +"'";
            if (cbR.Checked == true&&cbP.Checked == false )
            { sql += " and typeID=1"; }
            else if (cbP.Checked == true && cbR.Checked == false)
            { sql += " and TypeID=2"; }
            else if ((cbP.Checked == true && cbR.Checked == true) || (cbP.Checked == false  && cbR.Checked == false ))
            { }
            Fee_templetManage ftm = new Fee_templetManage();
            ds = ftm.GetList(sql);
            gvFeeTemplate.DataSource = ds;
            gvFeeTemplate.DataBind();
 
        }
       
      
        
        /// <summary>
        /// 选择编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvFeeTemplate_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            Session["feetemplet_ID"] = gvFeeTemplate.Rows[e.NewSelectedIndex].Cells[1].Text.ToString();
            Session["templetName"] = gvFeeTemplate.Rows[e.NewSelectedIndex].Cells[2].Text.ToString();
            Session["goodsType"] = gvFeeTemplate.Rows[e.NewSelectedIndex].Cells[5].Text.ToString();
            Session["typeID"] = gvFeeTemplate.Rows[e.NewSelectedIndex].Cells[6].Text.ToString();
            Response.Redirect("feeTemplateDesign.aspx");
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Session["feetemplet_ID"] = null;
            Response.Redirect("feeTemplateDesign.aspx");
        }

        protected void gvFeeTemplate_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            e.Row.Cells[5].Visible = false;//让第5,6列不可见
            e.Row.Cells[6].Visible = false;

            //如果是绑定数据行         
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.Cells[8].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除：\"" + e.Row.Cells[2].Text + "\"吗?')");
                }
            } 
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvFeeTemplate_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = Convert.ToInt32(gvFeeTemplate.DataKeys[e.RowIndex].Value);
            Fee_templetManage ftm=new Fee_templetManage ();
            Fee_templetdetailManage ftdm=new Fee_templetdetailManage ();
          
            if (ftm.Delete(id))
            {
                ftdm.Delete(id);
                MessageBox.ShowAndRedirect(this, "删除成功!", "feeTemplateView.aspx");
                gvFeeTemplate.DataSource = ds;
                gvFeeTemplate.DataBind();
            }
            else
                MessageBox.ShowAndRedirect(this, "删除失败!", "feeTemplateView.aspx");
               
        }

     


        /// <summary>
        /// 获取货物类别
        /// </summary>
        /// <returns></returns>
        public DataTable GetGoodsType()
        {
            CodeManage codeManage = new CodeManage();
            ds = codeManage.GetList(" codeType='goodType'");
            DataTable dt = ds.Tables[0];
            return dt;

        }
    }
}